package com.cssw.fyzb.core.dao.acct;

import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.util.DateUtil;
import com.cssw.fyzb.core.util.StringUtil;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhouxu
 * @create 2020-12-24 15:31
 */
@Repository
public class RedPackDaoImpl implements RedPackDao {
    @Override
    public Long saveLiveRedPack(Long userId, Long liveId, Integer num, BigDecimal money, String title, String effTime) throws ParseException {
        CmfLiveRedPack redPack = new CmfLiveRedPack();
        redPack.setFromUserId(userId);
        redPack.setLiveId(liveId);
        redPack.setNum(num);
        redPack.setRestNum(num);
        redPack.setMoney(money);
        redPack.setTitle(title);
        Date now = new Date();
        Date eff = new Date(now.getTime() + 5 * 60 * 1000);
        if (StringUtil.isNotEmpty(effTime)) {
            eff = DateUtil.parse(effTime);
        }
        redPack.setSendTime(now);
        redPack.setDayTime(TimeUtil.getTimeYYMMDD(now));
        redPack.setSurplus(money);
        redPack.setReceiveNum(0);
        redPack.setStatus(CmfLiveRedPack.STATUS_READY);
        redPack.setEffTime(eff);

        redPack.save();
        return redPack.getRedPackId();
    }

    @Override
    public void batchLiveRedPackSub(List<CmfLiveRedPackSub> subList) {
        Db.batchSave(subList, 1000);
    }

    @Override
    public Long savePersonRedPack(Long userId, Long otherId, BigDecimal money, String title) {
        RedPackPerson redPack = new RedPackPerson();
        redPack.setFromUserId(userId);
        redPack.setToUserId(otherId);
        redPack.setMoney(money);
        redPack.setTitle(title);
        redPack.setStatus(RedPackPerson.STATUS_READY);
        Date now = new Date();
        redPack.setSendTime(now);
        redPack.setDayTime(TimeUtil.getTimeYYMMDD(now));
        redPack.save();
        return redPack.getRedPackId();
    }

    @Override
    public RedPackPerson getPerRedPack(Long redPackId) {
        return RedPackPerson.dao.findById(redPackId);
    }

    @Override
    public void updatePerRedPackStatus(RedPackPerson pack, String status) {
        if (pack != null) {
            pack.setStatus(status);
            pack.update();
        }
    }

    @Override
    public CmfLiveRedPack getLiveRedPack(Long redPackId) {
        return CmfLiveRedPack.dao.findById(redPackId);
    }

    @Override
    public void updateLiveRedPackStatus(CmfLiveRedPack pack, String status) {
        if (pack != null) {
            pack.setStatus(status);
            pack.update();
        }
    }

    @Override
    public CmfLiveRedPackSub findFirstByStatus(Long redPackId, String status) {
        String sql = "select * from cmf_live_red_pack_sub where red_pack_id = ? and status = ? ";
        return CmfLiveRedPackSub.dao.findFirst(sql, redPackId, status);
    }

    @Override
    public void takeLiveRedPackSub(CmfLiveRedPackSub sub, Long userId) {
        if (sub != null) {
            sub.setUserId(userId);
            Date now = new Date();
            sub.setReceiveTime(now);
            sub.setStatus("2");
            sub.setDayTime(TimeUtil.getTimeYYMMDD(now));
            sub.update();
        }
    }

    @Override
    public Page<RedPackLog> listLog(Integer page, Integer limit, Map<String, Object> param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from t_red_pack_log where user_id=:userId");
        sqlParse.addSQL(" order by create_time desc ");

        return RedPackLog.dao.paginate(page, limit, sqlParse);
    }

    @Override
    public Long addRedPackLog(Long userId, String packType, String changeType, BigDecimal money, String tradeId, String tradeType, String tradeName) {
        RedPackLog redPackLog = new RedPackLog();
        redPackLog.setUserId(userId);
        redPackLog.setPackType(packType);
        redPackLog.setChangeType(changeType);
        redPackLog.setMoney(money);
        redPackLog.setTradeId(tradeId);
        redPackLog.setTradeType(tradeType);
        redPackLog.setTradeName(tradeName);
        redPackLog.setCreateTime(new Date());
        redPackLog.save();
        return redPackLog.getLogId();

    }

    @Override
    public void updateLiveRedPackSurplus(CmfLiveRedPack pack, BigDecimal surplus) {
        pack.setSurplus(surplus);
        pack.setRestNum(pack.getRestNum() - 1);
        pack.setReceiveNum(pack.getReceiveNum() + 1);
        pack.update();
    }

    @Override
    public CmfLiveRedPack queryLatestLivePack(Long liveId) {
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        param.put("liveId", liveId);
        sqlParse.addSQL(" select * from cmf_live_red_pack where live_id=:liveId");
        sqlParse.addSQL(" and status = '1'");
        sqlParse.addSQL(" order by send_time desc ");
        return CmfLiveRedPack.dao.findFirst(sqlParse);
    }

    @Override
    public List<RedPackPerson> listRedPackRerson() {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from t_red_pack_person where status=? ");
        return RedPackPerson.dao.find(sql.toString(), "1");
    }

    @Override
    public Long saveRedPackLog(Long userId, String packType, String changeType, BigDecimal money, String tradeId,
                               String tradeType, String tradeTName) {
        RedPackLog log = new RedPackLog();
        log.setUserId(userId);
        log.setPackType(packType);
        log.setChangeType(changeType);
        log.setMoney(money);
        log.setTradeId(tradeId);
        log.setTradeType(tradeType);
        log.setTradeName(tradeTName);
        log.setCreateTime(new Date());
        log.save();
        return log.getLogId();
    }

    @Override
    public List<CmfLiveRedPack> listCmfLiveRedPack() {
        StringBuilder str = new StringBuilder();
        str.append("select * from cmf_live_red_pack where status in (?,?) ");
        return CmfLiveRedPack.dao.find(str.toString(), "1", "2");
    }

    @Override
    public void updateStatus(Long redPackId, String status) {
        StringBuilder sql = new StringBuilder();
        sql.append("update t_red_pack_person set status=? where red_pack_id=?");
        Db.update(sql.toString(), status, redPackId);
    }

    @Override
    public void updateLiveStatus(Long redPackId, String status) {
        StringBuilder sql = new StringBuilder();
        sql.append("update cmf_live_red_pack set status=? where red_pack_id=?");
        Db.update(sql.toString(), status, redPackId);
    }

    @Override
    public RedPackPerson getRedPackPerson(Long redId, String tradeType) {
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        param.put("redId", redId);
        sqlParse.addSQL(" select * from t_red_pack_person where red_pack_id=:redId");
        return RedPackPerson.dao.findFirst(sqlParse);
    }

    @Override
    public CmfLiveRedPack getCmfLiveRedPack(Long redId, String tradeType) {
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        param.put("redId", redId);
        sqlParse.addSQL(" select * from cmf_live_red_pack t1  ");
        sqlParse.addSQL(" where t1.red_pack_id=:redId ");
        return CmfLiveRedPack.dao.findFirst(sqlParse);
    }

    @Override
    public List<CmfLiveRedPackSub> listUser(Long redId) {
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        param.put("redId", redId);
        sqlParse.addSQL(" select t1.*,(select t2.nick_name from t_user t2 where t2.user_id=t1.user_id)nick_name from cmf_live_red_pack_sub t1  ");
        sqlParse.addSQL(" where red_pack_id=:redId ");
        return CmfLiveRedPackSub.dao.find(sqlParse);
    }

    public CmfLiveRedPack getLiveRed(Long uid) {
        StringBuilder str = new StringBuilder();
        str.append("select * from cmf_live_red_pack where from_user_id=? and status in (1,2) order by red_pack_id desc ");
        return CmfLiveRedPack.dao.findFirst(str.toString(), uid);
    }

}
